﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.Repository
{
    public class CreateIRepository
    {
        private string tableName;
        private string fileDir;
        private DataTable dataTable;
        private DataTable indexTable;
        public CreateIRepository(string tableName, string fileDir, DataTable dataTable, DataTable indexTable)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
            this.dataTable = dataTable;
            this.indexTable = indexTable;
        }

        public void Run()
        {
            var dataRows = new List<DataRow>();
            foreach (DataRow row in this.dataTable.Rows)
            {
                dataRows.Add(row);
            }
            var indexRows = new List<DataRow>();
            foreach (DataRow row in this.indexTable.Rows)
            {
                indexRows.Add(row);
            }
            var entityName = Helper.GetPascalCase(tableName);
            var fileName = $"I{entityName}Repository";
            IList<string> contents = new List<string>();
            contents.Add($"using Hualong.TestWorldPro.Entities;");
            contents.Add($"");
            contents.Add($"namespace Hualong.TestWorldPro.Repository.{entityName}");
            contents.Add("{");
            contents.Add($"    public partial interface {fileName} : IBaseRepository");
            contents.Add("    {");
            contents.Add($"        Task<IList<{entityName}Entity>> GetItemsAsync();");
            var indexRowGroups = indexRows.GroupBy(xx => xx[2].ToString()).ToList();
            foreach (var indexRowGroup in indexRowGroups)
            {
                if (indexRowGroup.Count() == 1 && indexRowGroup.FirstOrDefault()[4].ToString() == "id")
                    continue;
                var rows = indexRowGroup.OrderBy(xx => xx[3]).ToList();
                string methodName = string.Empty;
                string parasName = string.Empty;
                string returnName = string.Empty;
                string complex = string.Empty;
                for (int i = 0; i < rows.Count; i++)
                {
                    DataRow field = dataRows.FirstOrDefault(xx => xx[0].ToString() == rows[i][4].ToString());
                    methodName += $"{Helper.GetPascalCase(rows[i][4].ToString())}";
                    parasName += $"{DataTypeMap.Get(field[1].ToString())} {Helper.GetCamelCase(rows[i][4].ToString())}";
                    if (i < rows.Count - 1)
                    {
                        methodName += "And";
                        parasName += ", ";
                    }
                }
                if (rows.FirstOrDefault()[1].ToString() == "0")
                {
                    returnName = $"Task<{entityName}Entity>";
                }
                else
                {
                    returnName = $"Task<IList<{entityName}Entity>>";
                    complex = "s";
                }
                contents.Add($"        {returnName} GetItem{complex}By{methodName}Async({parasName});");
                //delete
                contents.Add($"        Task<int> Delete{complex}By{methodName}Async({parasName});");
            }
            contents.Add($"        Task<{entityName}Entity> GetItemAsync(long id);");
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, entityName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{fileName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);

            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
